package com.irdstudio.apicenter.gateway.comm.tcp;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.irdstudio.apicenter.gateway.comm.MaccsChannelClient;
import com.irdstudio.apicenter.gateway.comm.MaccsPacket;
import com.irdstudio.apicenter.gateway.comm.MaccsPacketUtil;
import com.irdstudio.apicenter.gateway.comm.pack.AbstracePackProcessor;
import com.irdstudio.apicenter.gateway.core.dm.AppChannelForward;
import com.irdstudio.apicenter.gateway.util.SocketUtil;
import com.irdstudio.apicenter.gateway.util.spring.BeanUtils;
import com.irdstudio.apicenter.gateway.web.MaccsTradeDefineVo;

/**
 * 实现MACCS系统下的TCP/IP渠道通讯 根据指定的渠道定义信息进行初始化并进行通讯
 * 
 * @author guangming.li
 * @version 1.0
 * @date 2015-09-17
 */
@Service(value = "TCP")
public class MaccsSocketClient implements MaccsChannelClient {

	/* 渠道定义信息 */
	private AppChannelForward acf = null;

	/* 文本日志记录对象(Logback对象) */
	private final static Logger logger = LoggerFactory.getLogger(MaccsSocketClient.class);

	/**
	 * 传入渠道定义信息(创建时传入)
	 */
	@Override
	public void setChannelDefine(AppChannelForward acf) {
		this.acf = acf;
	}

	@Override
	public MaccsPacket sendAndWait(MaccsTradeDefineVo tradeDefine, MaccsPacket inUvo) {

		// 1.从配置中获取IP地址及端口
		// acf.setForwardChannelUrl("20.65.25.81:5002");
		//acf.setForwardChannelUrl("20.4.16.22:5002");
		//acf.setForwardChannelUrl("192.168.1.109:6005");//群鹏
		// acf.setForwardChannelUrl("192.168.19.188:6005");//福生
		// acf.setForwardChannelUrl("10.17.0.64:5002");// 福生
		String[] connectInfo = acf.getForwardChannelUrl().split(":");
		if (connectInfo.length != 2) {
			logger.info("TCP渠道定义格式为：IP地址:端口");
			return MaccsPacketUtil.makeFailurePacket("TCP渠道地址定义错误!");
		}
		String ip = connectInfo[0];
		int port = Integer.valueOf(connectInfo[1]);
		logger.info("TCP渠道：" + getChannelDesc());
		logger.info("发送信息(原始):" + inUvo.toString());

		// 2.准备交易输入数据,如果有配置输入格式化信息，则按格式化配置对数据进行格式化
		if (!(tradeDefine.getInList() == null || tradeDefine.getInList().size() == 0)) {
			inUvo = MaccsPacketUtil.makeFormatPacket(tradeDefine.getInList(), inUvo);
		}

		// 3.获取组/解包处理类
		AbstracePackProcessor packProcessor = (AbstracePackProcessor) BeanUtils.getBean(acf.getPackProcessorClass());

		// 4.组合成要发送的报文
		String sendMessage = packProcessor.getSendPackage(acf, tradeDefine, inUvo);
		// sendMessage
		// ="017660<?xml version=\"1.0\" encoding=\"UTF-8\"
		// ?><root><head><tranCode>100031</tranCode><applSeq>20151126120947259</applSeq><tranDate>2016-02-14</tranDate><tranTime>12:07:37</tranTime><chlGrp>0001</chlGrp><bchCde>900000</bchCde><version>1.0</version><successCode>E0000</successCode><errorMessage>交易成功</errorMessage></head><body><approvearray><approve><psperdno>3</psperdno><psduedt>2016-02-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>283.9</psprcpamt><psnormintamt>99.23</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>9319.43</psremprcp><setlprcp>283.9</setlprcp><setlnormint>99.23</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>Y</setlind></approve><approve><psperdno>4</psperdno><psduedt>2016-03-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>293.04</psprcpamt><psnormintamt>90.09</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>9026.39</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>5</psperdno><psduedt>2016-04-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>289.86</psprcpamt><psnormintamt>93.27</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>8736.53</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>6</psperdno><psduedt>2016-05-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>295.76</psprcpamt><psnormintamt>87.37</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>8440.77</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>7</psperdno><psduedt>2016-06-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>295.91</psprcpamt><psnormintamt>87.22</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>8144.86</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>8</psperdno><psduedt>2016-07-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>301.68</psprcpamt><psnormintamt>81.45</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>7843.18</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>9</psperdno><psduedt>2016-08-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>302.08</psprcpamt><psnormintamt>81.05</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>7541.1</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>10</psperdno><psduedt>2016-09-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>305.21</psprcpamt><psnormintamt>77.92</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>7235.89</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>11</psperdno><psduedt>2016-10-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>310.77</psprcpamt><psnormintamt>72.36</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>6925.12</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>12</psperdno><psduedt>2016-11-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>311.57</psprcpamt><psnormintamt>71.56</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>6613.55</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>13</psperdno><psduedt>2016-12-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>316.99</psprcpamt><psnormintamt>66.14</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>6296.56</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>14</psperdno><psduedt>2017-01-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>318.07</psprcpamt><psnormintamt>65.06</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>5978.49</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>15</psperdno><psduedt>2017-02-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>321.35</psprcpamt><psnormintamt>61.78</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>5657.14</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>16</psperdno><psduedt>2017-03-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>330.33</psprcpamt><psnormintamt>52.8</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>5326.81</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>17</psperdno><psduedt>2017-04-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>328.09</psprcpamt><psnormintamt>55.04</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>4998.72</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>18</psperdno><psduedt>2017-05-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>333.14</psprcpamt><psnormintamt>49.99</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>4665.58</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>19</psperdno><psduedt>2017-06-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>334.92</psprcpamt><psnormintamt>48.21</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>4330.66</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>20</psperdno><psduedt>2017-07-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>339.82</psprcpamt><psnormintamt>43.31</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>3990.84</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>21</psperdno><psduedt>2017-08-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>341.89</psprcpamt><psnormintamt>41.24</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>3648.95</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>22</psperdno><psduedt>2017-09-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>345.42</psprcpamt><psnormintamt>37.71</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>3303.53</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>23</psperdno><psduedt>2017-10-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>350.09</psprcpamt><psnormintamt>33.04</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>2953.44</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>24</psperdno><psduedt>2017-11-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>352.61</psprcpamt><psnormintamt>30.52</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>2600.83</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>25</psperdno><psduedt>2017-12-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>357.12</psprcpamt><psnormintamt>26.01</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>2243.71</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>26</psperdno><psduedt>2018-01-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>359.94</psprcpamt><psnormintamt>23.19</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>1883.77</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>27</psperdno><psduedt>2018-02-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>363.66</psprcpamt><psnormintamt>19.47</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>1520.11</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>28</psperdno><psduedt>2018-03-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>368.94</psprcpamt><psnormintamt>14.19</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>1151.17</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>29</psperdno><psduedt>2018-04-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>371.23</psprcpamt><psnormintamt>11.9</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>779.94</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>30</psperdno><psduedt>2018-05-08</psduedt><psinstmamt>383.13</psinstmamt><psprcpamt>375.33</psprcpamt><psnormintamt>7.8</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>404.61</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>31</psperdno><psduedt>2018-06-08</psduedt><psinstmamt>408.79</psinstmamt><psprcpamt>404.61</psprcpamt><psnormintamt>4.18</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>0</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>2</psperdno><psduedt>2016-01-08</psduedt><psinstmamt>167.86</psinstmamt><psprcpamt>167.86</psprcpamt><psnormintamt>0</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>9603.33</psremprcp><setlprcp>167.86</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>Y</setlind></approve><approve><psperdno>0</psperdno><psduedt>2015-12-08</psduedt><psinstmamt>0</psinstmamt><psprcpamt>0</psprcpamt><psnormintamt>0</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>10000</psremprcp><setlprcp>0</setlprcp><setlnormint>0</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>N</setlind></approve><approve><psperdno>1</psperdno><psduedt>2016-01-08</psduedt><psinstmamt>332.14</psinstmamt><psprcpamt>228.81</psprcpamt><psnormintamt>103.33</psnormintamt><psodintamt>0</psodintamt><pscommodint>0</pscommodint><psremprcp>9771.19</psremprcp><setlprcp>228.81</setlprcp><setlnormint>103.33</setlnormint><setlodintamt>0</setlodintamt><setlcommodint>0</setlcommodint><psfee>0</psfee><setlfee>0</setlfee><psfeeamt>0</psfeeamt><setlfeeamt>0</setlfeeamt><pswvnmint>0</pswvnmint><pswvodint>0</pswvodint><pswvcommint>0</pswvcommint><setlind>Y</setlind></approve></approvearray></body></root>";

		// 5.连接服务端进行发送并接收返回信息
		MaccsPacket backPacket = null;
		Socket socket = null;
		OutputStream socketOut = null;
		PrintWriter pw = null;
		InputStream socketIn = null;
		try {
			socket = new Socket(ip, port);
			// 发送报文
			socketOut = socket.getOutputStream();
			pw = new PrintWriter(socketOut, true);
			pw.println(sendMessage);
			pw.flush();

			// 先接收所有报文
			socketIn = socket.getInputStream();
			byte[] allBytes = SocketUtil.readPackage(socketIn, this.acf);
			String backMsg = new String(allBytes, acf.getChannelCharset());

			// 7.将获得的数据转换为maccs下的数据包
			backPacket = packProcessor.getReadPackage(acf, tradeDefine, backMsg);

		} catch (Exception e) {
			e.printStackTrace();
			backPacket = MaccsPacketUtil.makeFailurePacket(e.getMessage());
		} finally {
			try {
				if (pw != null)
				{
					pw.close();
				}
			} catch (Exception e2) {}
			try {
				if (socketOut != null)
				{
					socketOut.close();
				}
			} catch (Exception e2) {}
			try {
				if (socketIn != null)
				{
					socketIn.close();
				}
			} catch (Exception e2) {}
			
			try {
				if (socket != null)
				{
					socket.close();
				}
			} catch (Exception e) {
//				e.printStackTrace();s
			}
		}

		// 将返回信息组成UVO包返回给调用程序
		return backPacket;
	}

	/**
	 * 返回渠道信息描述
	 * 
	 * @return
	 */
	private String getChannelDesc() {
		StringBuffer sb = new StringBuffer();
		sb.append(acf.getForwardChannelName()).append("(");
		sb.append(acf.getForwardChannelId()).append(")");
		sb.append(",通讯地址:").append(acf.getForwardChannelUrl());
		sb.append(",字符编码:").append(acf.getChannelCharset());
		return sb.toString();
	}
}
